﻿using System;
using System.Windows;
using System.Windows.Media.Animation;
using HeritageAcademy.Models.ViewModels;

namespace HeritageAcademy.Window
{
	/// <summary>
	/// Window content class.
	/// </summary>
	public partial class WindowContent
    {
        #region Parameters
        
        FrameworkElement _currentView;
        TestWizardViewModel _viewModel;
        ResultsViewModel _resultsViewModel;

        #endregion

        #region Ctor

        public WindowContent()
		{
			InitializeComponent();
            Loaded += WindowContent_Loaded;
		}

        #endregion

        #region Private Methods

        void InitializeTestWizard()
        {
            _viewModel = new TestWizardViewModel();
            _viewModel.RequestClose += _viewModel_RequestClose;
            viewTestWizard.DataContext = _viewModel;
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Window loaded event handler
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Routed event handler</param>
        void WindowContent_Loaded(object sender, RoutedEventArgs e)
        {
            _currentView = viewWellcomeScreen;

            viewWellcomeScreen.StartButtonClick = StartButton_Click;
            viewTestWizard.ReturnBackButton.Click += ReturnBackButton_Click;
            viewResultsScreen.ReturnBackButton.Click += ReturnBackButton_Click;
        }

        /// <summary>
        /// Start button click event handler.
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Routed event arguments</param>
        void StartButton_Click(object sender, RoutedEventArgs e)
        {
            InitializeTestWizard(); // TODO: SPEED UP - move to test selector when implemented
            viewTestWizard.dtDurationTime.Start();
            ChangeView(viewTestWizard);
        }

        /// <summary>
        /// Return back button click event handler.
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Routed event arguments</param>
        void ReturnBackButton_Click(object sender, RoutedEventArgs e)
        {
            viewTestWizard.dtDurationTime.Stop();
            viewTestWizard.IsChanged = false;           
            ChangeView(viewWellcomeScreen);
        }

        /// <summary>
        /// Last test page next button click event handler.
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Event arguments</param>
        void _viewModel_RequestClose(object sender, EventArgs e)
        {
            viewTestWizard.FreezLastState();
            viewTestWizard.dtDurationTime.Stop();

            _resultsViewModel = new ResultsViewModel();
            _resultsViewModel.InitializeResults();
            viewResultsScreen.DataContext = _resultsViewModel;

            ChangeView(viewResultsScreen);
        }

        #endregion

        #region Change View
        /// <summary>
        /// Change current view.
        /// </summary>
        /// <param name="newView">New view (framework element)</param>
        public void ChangeView(FrameworkElement newView)
        {
            var fadeOutContent = FindResource("fadeOut") as Storyboard;
            if (fadeOutContent != null)
            {
                fadeOutContent = fadeOutContent.Clone();
                fadeOutContent.Completed += fadeOutContent_Completed;
                fadeOutContent.Begin(_currentView);
            }

            _currentView = newView;
        }

        /// <summary>
        /// Fade out content completed event handler.
        /// Start new view fade in when the old view fade out ends.
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Event arguments</param>
        void fadeOutContent_Completed(object sender, EventArgs e)
        {
            var fadeOutContent = FindResource("fadeOut") as Storyboard;
            if (fadeOutContent != null)
            {
                fadeOutContent = fadeOutContent.Clone();
                fadeOutContent.Completed -= fadeOutContent_Completed;
            }

            var fadeInContent = FindResource("fadeIn") as Storyboard;
            if (fadeInContent != null) fadeInContent.Begin(_currentView);
        }
        #endregion 
	}
}